package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析logic组件、action组件配置文件中“判断结点”的解析类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 温建
 * @version 1.0 2008.8.18
 * 
 * @see <code>DecisionNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.DecisionNodeInfo;

public class DecisionNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_DECISION = "decision";

	public static final String KEY_DECISION_IF = "if";

	public static final String KEY_DECISION_IF_EXPR = "expr";

	public static final String KEY_DECISION_IF_THEN = "then";

	public static final String KEY_DECISION_IF_ELSE = "else";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> decisionNodeElements = root
				.elements(KEY_NODETYPE_DECISION);

		List<INodeInfo> decisionNodeInfos = new ArrayList<INodeInfo>(
				decisionNodeElements.size());

		for (Element decisionNodeElement : decisionNodeElements) {
			String id = decisionNodeElement.attributeValue(KEY_NODE_ID).trim();
			String x = decisionNodeElement.attributeValue(KEY_NODE_X).trim();
			String y = decisionNodeElement.attributeValue(KEY_NODE_Y).trim();
			String name = decisionNodeElement.elementTextTrim(KEY_NODE_NAME);
			String description = decisionNodeElement
					.elementTextTrim(KEY_NODE_DESCRIPTION);

			Element testElement = decisionNodeElement.element(KEY_DECISION_IF);
			String expr = testElement.elementTextTrim(KEY_DECISION_IF_EXPR);
			String then = testElement.attributeValue(KEY_DECISION_IF_THEN)
					.trim();

			String elseId = testElement.attributeValue(KEY_DECISION_IF_ELSE)
					.trim();

			logger.debug("  load decision node id = \"" + id + "\" name = \""
					+ name + "\" description = \"" + description
					+ "\" expression = \"" + expr + "\"");
			DecisionNodeInfo decisionNodeInfo = new DecisionNodeInfo(id, name,
					description, expr, then, elseId, x, y);

			decisionNodeInfos.add(decisionNodeInfo);
		}

		return decisionNodeInfos;
	}

}
